﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX.AudioVideoPlayback;
using System.IO;

namespace PControl
{
    /// <summary>
    /// 自定义视频播放控件
    /// </summary>
    public partial class VideoPlayer : Panel
    {
        /// <summary>
        /// 视频文件路径
        /// </summary>
        private String filePath;
        /// <summary>
        /// 文件路径是否合法
        /// </summary>
        private bool isFileLegal;
        /// <summary>
        /// 视频总时长 单位:s
        /// </summary>
        private int maxTime;
        /// <summary>
        /// 视频播放当前时间 单位:s
        /// </summary>
        private int currentTime = 0;
        /// <summary>
        /// 视频播放器
        /// </summary>
        private Video video = null;
        /// <summary>
        /// 是否正在播放
        /// </summary>
        private bool isPlayinig;
        /// <summary>
        /// 是否循环播放
        /// </summary>
        private bool isCycle=true;

        /// <summary>
        /// 设置视频资源文件路径
        /// </summary>
        [Browsable(true)]
        public String FilePath
        {
            get { return filePath; }
            set 
            {
                filePath = value;
                IsFileLegal = File.Exists(filePath);
            }
        }
        /// <summary>
        /// 文件资源是否可播放
        /// </summary>
        [Browsable(false)]
        public bool IsFileLegal
        {
            get { return isFileLegal; }
            set 
            { 
                isFileLegal = value;
                if (isFileLegal)
                {
                    int height = this.Height;
                    int width = this.Width;
                    try
                    {
                        if (video != null)
                        {
                            video.Dispose();
                        }
                        video = new Video(filePath);
                        // 把Video文件分配给创建的Panel组件
                        video.Owner = this;
                        // 以记录的panel组件的大小来重新定义
                        this.Width = width;
                        this.Height = height;
                        //// 播放AVI文件的第一帧，主要是为了在panel中显示
                        //video.Play();
                        //video.Pause();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
        }
        /// <summary>
        /// 是否循环播放
        /// </summary>
        [DefaultValue(true)]
        public bool IsCycle
        {
            get { return isCycle; }
            set { isCycle = value; }
        }
        public VideoPlayer()
        {
            InitializeComponent();
        }

        private void VideoPlayer_Validated(object sender, EventArgs e)
        {
            tmrTime_Tick(sender, e);
            tmrTime.Stop();
        }
        //播放时间显示控制计时器
        private void tmrTime_Tick(object sender, EventArgs e)
        {
            if (isPlayinig)
            {
                currentTime++;
                if (currentTime == maxTime)
                {
                    if (isCycle)
                    {
                        video.Stop();
                        video.Play();
                        currentTime = 0;
                    }
                    else
                    {
                        isPlayinig = false;
                        video.Stop();
                        tmrTime.Stop();
                    }
                }
            }
        }
        /// <summary>
        /// 启动播放器
        /// </summary>
        public void Start()
        {
            if (IsFileLegal)
            {
                video.Play();
                currentTime = 0;
                maxTime = (int)video.Duration;
                isPlayinig = true;
                tmrTime.Start();
            }
        }
        /// <summary>
        /// 停止播放器
        /// </summary>
        public void Stop()
        {
            video.Stop();
        }
    }
}
